package com.yanqu.road.dao.impl.union.cross.huaben;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.huaben.UnionHuaBenBaseTempData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionHuaBenBaseTempDataDaoImpl extends TempDataObjectDao<UnionHuaBenBaseTempData> {


    public static String getUnionHuaBenBaseTempDataSql = "select * from `t_c_hua_ben_base_temp` where `weekId`=? or `weekId` = ?";
    public Map<Integer, Map<Long, Map<Integer, Map<Integer, UnionHuaBenBaseTempData>>>> getUnionHuaBenBaseTempData(int preWeekId, int weekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Long, Map<Integer, Map<Integer, UnionHuaBenBaseTempData>>>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getUnionHuaBenBaseTempDataSql);
            int i = 1;
            call.setInt(i++, preWeekId);
            call.setInt(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionHuaBenBaseTempData data = parse(rs);
                if(result.get(data.getWeekId()) == null){
                    result.put(data.getWeekId(), new ConcurrentHashMap<>());
                }
                Map<Long, Map<Integer, Map<Integer, UnionHuaBenBaseTempData>>> weekMap = result.get(data.getWeekId());

                if(weekMap.get(data.getServerId()) == null){
                    weekMap.put(data.getServerId(), new ConcurrentHashMap<>());
                }
                Map<Integer, Map<Integer, UnionHuaBenBaseTempData>> serverMap = weekMap.get(data.getServerId());
                if(serverMap.get(data.getDegree()) == null){
                    serverMap.put(data.getDegree(), new ConcurrentHashMap<>());
                }
                Map<Integer, UnionHuaBenBaseTempData> map = serverMap.get(data.getDegree());
                map.put(data.getLevel(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertUnionHuaBenBaseTempDataSql = "insert into `t_c_hua_ben_base_temp`(weekId,serverId,id,degree,level,bossHP,monsterHP,monsterWave,monsterReward,bossReward,bossSReward," +
            "probability,bossIcon,monsterIcon,bossScore,monsterScore) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    
    @Override
    protected boolean addData(UnionHuaBenBaseTempData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDegree()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getBossHP()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getMonsterHP()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMonsterWave()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMonsterReward()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBossReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getBossSReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getProbability()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBossIcon()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getMonsterIcon())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBossScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMonsterScore()));
        if (getDb().executeNoneQuery(insertUnionHuaBenBaseTempDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUnionHuaBenBaseTempDataSql = "update `t_c_hua_ben_base_temp` set degree=?,level=?,bossHP=?,monsterHP=?,monsterWave=?,monsterReward=?,bossReward=?,bossSReward=?," +
            "probability=?,bossIcon=?,monsterIcon=?,bossScore=?,monsterScore=? where weekId=? and serverId=? and id=?";
    
    @Override
    protected boolean updateData(UnionHuaBenBaseTempData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getDegree()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getBossHP()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getMonsterHP()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMonsterWave()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMonsterReward()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBossReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getBossSReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getProbability()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBossIcon()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getMonsterIcon())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBossScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMonsterScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getId()));
        if (getDb().executeNoneQuery(updateUnionHuaBenBaseTempDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UnionHuaBenBaseTempData parseData(ResultSet rs) throws SQLException {
        UnionHuaBenBaseTempData data = new UnionHuaBenBaseTempData();
        data.setWeekId(rs.getInt("weekId"));
        data.setServerId(rs.getLong("serverId"));
        data.setId(rs.getInt("id"));
        data.setDegree(rs.getInt("degree"));
        data.setLevel(rs.getInt("level"));
        data.setBossHP(rs.getString("bossHP"));
        data.setMonsterHP(rs.getString("monsterHP"));
        data.setMonsterWave(rs.getInt("monsterWave"));
        data.setMonsterReward(rs.getInt("monsterReward"));
        data.setBossReward(rs.getInt("bossReward"));
        data.setBossSReward(rs.getString("bossSReward"));
        data.setProbability(rs.getString("probability"));
        data.setBossIcon(rs.getInt("bossIcon"));
        data.setMonsterIcon(JSON.parseObject(rs.getString("monsterIcon"), new TypeReference<List<Integer>>() {}));
        data.setBossScore(rs.getInt("bossScore"));
        data.setMonsterScore(rs.getInt("monsterScore"));
        return data;
    }
}
